﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ScormModel;

namespace WpfELearningAuthoringTool
{
    class TreeViewProcessing
    {
        ImsManifest imsmanifest;
        TreeView tvMain;
        string lessonImagePath = "TaskIcon.png";
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public TreeView MainTree
        {
            get { return this.tvMain; }
            set { this.tvMain = value; }
        }
        public Boolean addModuleForNoneTemplate(TreeViewItem selectedItem,TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem lastItem = (TreeViewItem)selectedItem.Items[selectedItem.Items.Count - 1];
                selectedItem.Items.RemoveAt(selectedItem.Items.Count - 1);
                selectedItem.Items.Add(newNode);
                selectedItem.Items.Add(lastItem);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForNoneTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForNoneTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForCustomTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForCustomTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForCustomTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addTestForCustomTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addExamForCustomTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForLinearTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem lastItem = (TreeViewItem)selectedItem.Items[selectedItem.Items.Count - 1];
                selectedItem.Items.RemoveAt(selectedItem.Items.Count - 1);
                selectedItem.Items.Add(newNode);
                selectedItem.Items.Add(lastItem);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForLinearTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForLinearTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForLinearChoiceTemlate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem examNode = ScormModel.TreeUtil.GetTreeView("Exam For " + input.Text, lessonImagePath);
                newNode.Items.Add(examNode);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForLinearChoiceTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem lastItem = (TreeViewItem)selectedItem.Items[selectedItem.Items.Count - 1];
                selectedItem.Items.RemoveAt(selectedItem.Items.Count - 1);
                selectedItem.Items.Add(newNode);
                selectedItem.Items.Add(lastItem);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForLinearChoiceTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForConstrainedChoiceTemlate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem examNode = ScormModel.TreeUtil.GetTreeView("Exam For " + input.Text, lessonImagePath);
                newNode.Items.Add(examNode);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForConstrainedChoiceTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem lastItem = (TreeViewItem)selectedItem.Items[selectedItem.Items.Count - 1];
                selectedItem.Items.RemoveAt(selectedItem.Items.Count - 1);
                selectedItem.Items.Add(newNode);
                selectedItem.Items.Add(lastItem);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForConstrainedChoiceTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForLinearControlTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem lastItem = (TreeViewItem)selectedItem.Items[selectedItem.Items.Count - 1];
                selectedItem.Items.RemoveAt(selectedItem.Items.Count - 1);
                selectedItem.Items.Add(newNode);
                selectedItem.Items.Add(lastItem);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForLinearControlTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForLinearControlTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForCompetencyTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem lastItem = (TreeViewItem)selectedItem.Items[selectedItem.Items.Count - 1];
                TreeViewItem examPart = ScormModel.TreeUtil.GetTreeView("Exam for " + input.Text, lessonImagePath);
                lastItem.Items.Add(examPart);
                selectedItem.Items.RemoveAt(selectedItem.Items.Count - 1);
                selectedItem.Items.Add(newNode);
                selectedItem.Items.Add(lastItem);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForCompetencyTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForCompetencyTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeModuleForCompetencyTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                TreeViewItem parent = (TreeViewItem)selectedItem.Parent;
                int index = 0;
                for (int i = 0; i < parent.Items.Count; i++)
                {
                    if (parent.Items[i] == selectedItem)
                    {
                        index = i;
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                parent.Items.Remove(selectedItem);
                int realIndex = index - 2;
                if (realIndex > 0) {
                    TreeViewItem lastItem = (TreeViewItem)parent.Items[parent.Items.Count - 1];
                    lastItem.Items.RemoveAt(realIndex);
                }
                
                

                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForKnowledgePacedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem preTest = ScormModel.TreeUtil.GetTreeView(input.Text + " PreTest", lessonImagePath);
                TreeViewItem lessons = ScormModel.TreeUtil.GetTreeView(input.Text + " Lessons", lessonImagePath);
                TreeViewItem postTest = ScormModel.TreeUtil.GetTreeView(input.Text + " PostTest", lessonImagePath);
                newNode.Items.Add(preTest);
                newNode.Items.Add(lessons);
                newNode.Items.Add(postTest);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForKnowledgePacedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForKnowledgePacedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForKnowledgePacedReusedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                string selectItemName = imsmanifest.getNameOfTreeViewItem(selectedItem);
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                TreeViewItem preTest = ScormModel.TreeUtil.GetTreeView(input.Text + " PreTest", lessonImagePath);
                TreeViewItem lessons = ScormModel.TreeUtil.GetTreeView(input.Text + " Lessons", lessonImagePath);
                TreeViewItem postTest = ScormModel.TreeUtil.GetTreeView(input.Text + " PostTest", lessonImagePath);
                newNode.Items.Add(preTest);
                newNode.Items.Add(lessons);
                newNode.Items.Add(postTest);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForKnowledgePacedReusedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForKnowledgePacedReusedTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean addModuleForRemediationTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem root = imsmanifest.createManifestTreeViewItem();
                tvMain.Items.Clear();
                tvMain.Items.Add(root);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addLessonForRemediationTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean addQuestionForRemediationTemplate(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                TreeViewItem newNode = ScormModel.TreeUtil.GetTreeView(input.Text, lessonImagePath);
                selectedItem.Items.Add(newNode);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeItemForRemediationTemplate(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                TreeViewItem root = imsmanifest.createManifestTreeViewItem();
                tvMain.Items.Clear();
                tvMain.Items.Add(root);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public Boolean renameTreeViewItem(TreeViewItem selectedItem, TextBox input)
        {
            Boolean result = false;
            try
            {
                StackPanel sp = null;
                Label setectItemLabel = null;
                sp = (StackPanel)selectedItem.Header;
                foreach (var child in sp.Children)
                {
                    if (child.GetType().ToString() == "System.Windows.Controls.Label")
                    {
                        setectItemLabel = (Label)child;
                        setectItemLabel.Content = input.Text;
                        result = true;
                    }

                }
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        public Boolean removeTreeViewItem(TreeViewItem selectedItem)
        {
            Boolean result = false;
            try
            {
                TreeViewItem parent = (TreeViewItem)selectedItem.Parent;
                parent.Items.Remove(selectedItem);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public int findIndexOfTreeViewItem(TreeViewItem selectedItem)
        {
            TreeViewItem parentItem = (TreeViewItem)selectedItem.Parent;
            for (int i = 0; i < parentItem.Items.Count; i++)
            {
                if (parentItem.Items[i] == selectedItem)
                    return i;
                else
                    continue;
            }
            return -1;
        }
        public void swapItemToNext(TreeViewItem selectedItem)
        {
            if(selectedItem == null)
                return;
            int currentIndex = findIndexOfTreeViewItem(selectedItem);
            if (currentIndex >= 0)
            {
                TreeViewItem parentItem = (TreeViewItem)selectedItem.Parent;
                int nextIndex = currentIndex + 1;
                if (nextIndex == parentItem.Items.Count)
                    return;
                else {
                    parentItem.Items.RemoveAt(currentIndex);
                    parentItem.Items.Insert(nextIndex, selectedItem);
                    selectedItem.Focus();
                }
            }
        }

        public void swapItemToPrevious(TreeViewItem selectedItem)
        {
            if (selectedItem == null)
                return;
            int currentIndex = findIndexOfTreeViewItem(selectedItem);
            if (currentIndex >= 0)
            {
                TreeViewItem parentItem = (TreeViewItem)selectedItem.Parent;
                int preIndex = currentIndex - 1;
                if (preIndex < 0)
                    return;
                else
                {
                    parentItem.Items.RemoveAt(currentIndex);
                    parentItem.Items.Insert(preIndex, selectedItem);
                    selectedItem.Focus();
                }
            }
        }
    }
}
